package me.zingon.controller;

import com.alibaba.fastjson.JSON;
import me.zingon.cache.UserCache;
import me.zingon.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@Scope("session")
@RequestMapping("/jwc")
public class JwcController {

	@Autowired
	UserCache userCache;

	@RequestMapping(value="/getScore.ztc")
	@ResponseBody
	public Object getScore(@RequestParam(value="startPage",required=false,defaultValue="0")String startPage,
						   @RequestParam(value="pageSize",required=false,defaultValue="100")String pageSize,
						   @RequestParam(value = "yzm",required = false,defaultValue = "")String yzm,
						   HttpServletRequest req) throws IOException {
		HttpSession session=req.getSession();
		JSON o= (JSON) JSON.parse(userCache.getScore(startPage, pageSize,session));
		if(o.toString().length()<20) {
			userCache.evivtScore(startPage, pageSize, session);
		}
		return o;
	}

	@RequestMapping("/submitYzm.ztc")
	@ResponseBody
	public Object loginNtuWithYzm( @RequestParam(value = "yzm" )String yzm,
								   HttpServletRequest req) throws IOException {
		Map<String,String> result=new HashMap<>(1);
		result.put("result",userCache.loginNtuByYzm((User) req.getSession().getAttribute("user"),yzm));
		return result;
	}

	@RequestMapping("/getPjInfo.ztc")
	@ResponseBody
	public Object getPjInfo() throws IOException{
		return JSON.parse(userCache.getPjInfo());
	}

	@RequestMapping("/pj.ztc")
	public String pj(@RequestParam(value = "cjids",required = false,defaultValue ="")String cjids,@RequestParam(value="cjids",required=false,defaultValue="111111111")String choose) throws IOException{
		if(!cjids.equals(""))
			userCache.pj(cjids, choose);
		return "redirect:/pj.pj";
	}
}
